package cn.edu.dlut.WingGameDemos;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import cn.edu.dlut.WingGame.Game.WingActivity;
import cn.edu.dlut.WingGame.Game.WingGame;
import cn.edu.dlut.WingGame.Sound.SoundFile;
import cn.edu.dlut.WingGame.Sound.WingMusic;
import cn.edu.dlut.WingGame.Sound.WingSound;

public class SoundTest extends WingActivity {

	private AudioManager am;
	private WingSound mySound = new WingSound();
	private SoundFile[] myFile;

	private static Activity instance;
	private int currentVol, maxVolume;
	private boolean ON = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		WingGame.initWingGame(this);
		WingGame.setFPS(60, false);
		WingGame.setShowFPS(true);
		instance = this;

		am = (AudioManager) SoundTest.instance
				.getSystemService(Context.AUDIO_SERVICE);
		maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		currentVol = am.getStreamVolume(AudioManager.STREAM_MUSIC);
		this.setVolumeControlStream(1);

		WingMusic.play();
		WingMusic.play("audio/Penguino.mp3", true);
		WingMusic.getMediaPlayer().setVolume(1, 1);

		mySound.createSoundFile("audio/PenguinSqueak1.ogg");
		mySound.createSoundFile("audio/PenguinSqueak2.ogg");
		mySound.createSoundFile("audio/SnowThrow1.ogg");
		mySound.createSoundFile("audio/SnowThrow2.ogg");
		myFile = mySound.getSounds();

		// WingMusic.getMediaPlayer().setLooping(true);
	}

	// private void logic() {
	// currentVol = am.getStreamVolume(AudioManager.STREAM_MUSIC);// 不断获取当前的音量值
	// }

	@Override
	public boolean onKeyDown(int key, KeyEvent event) {

		if (key == KeyEvent.KEYCODE_DPAD_CENTER) {
			ON = !ON;
			if (ON == false)
				WingMusic.pause();
			else
				WingMusic.play();
			myFile[0].play();
		} else if (key == KeyEvent.KEYCODE_DPAD_UP) {
			WingMusic.getMediaPlayer().seekTo(
					WingMusic.getMediaPlayer().getCurrentPosition() + 5000);
		} else if (key == KeyEvent.KEYCODE_DPAD_DOWN) {
			if (WingMusic.getMediaPlayer().getCurrentPosition() < 5000) {
				WingMusic.getMediaPlayer().seekTo(0);
			} else {
				WingMusic.getMediaPlayer().seekTo(
						WingMusic.getMediaPlayer().getCurrentPosition() - 5000);
			}
		} else if (key == KeyEvent.KEYCODE_DPAD_LEFT) {
			currentVol += 1;
			if (currentVol > maxVolume) {
				currentVol = 100;
			}
			am.setStreamVolume(AudioManager.STREAM_MUSIC, currentVol,
					AudioManager.FLAG_PLAY_SOUND);
			myFile[1].play();
		} else if (key == KeyEvent.KEYCODE_DPAD_RIGHT) {
			currentVol -= 1;
			if (currentVol <= 0) {
				currentVol = 0;
			}
			am.setStreamVolume(AudioManager.STREAM_MUSIC, currentVol,
					AudioManager.FLAG_PLAY_SOUND);
			myFile[2].play();
		}
		myFile[3].play();
		// soundPool.play(id, currentVol, currentVol, 1, 0, 1f);
		// soundPool.play(soundPoolMap.get(1), currentVol, currentVol, 1, 0,
		// 1f);//备注4
		// soundPool.pause(1);//暂停SoundPool的声音
		return super.onKeyDown(key, event);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return true;
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		WingMusic.stop();
	}

}
